package com.iswust.visual.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iswust.visual.mapper.BayonetDataMapper;
import com.iswust.visual.mapper.CityAqiMapper;
import com.iswust.visual.model.BayonetData;
import com.iswust.visual.model.vo.DateTimeVo;
import com.iswust.visual.model.vo.MainViewVo;
import com.iswust.visual.model.vo.SankeyVo;
import com.iswust.visual.model.vo.ScatterData;
import com.iswust.visual.service.BayonetDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author swust
 * @since 2023-02-17
 */
@Service
public class BayonetDataServiceImpl extends ServiceImpl<BayonetDataMapper, BayonetData> implements BayonetDataService {

    @Autowired
    private CityAqiMapper cityAqiMapper;

    @Autowired
    private BayonetDataMapper bayonetDataMapper;

    @Override
    public List<ScatterData> getByDay(DateTimeVo dateTimeVo) {
        final List<ScatterData> scatterDataByDate = cityAqiMapper.getScatterDataByDay(dateTimeVo);
        return scatterDataByDate;
    }

    @Override
    public List<ScatterData> getByMonth(DateTimeVo dateTimeVo) {
        return cityAqiMapper.getScatterDataByMonth(dateTimeVo);
    }

    @Override
    public SankeyVo getBayonetByDate(DateTimeVo dateTimeVo) {
        final List<BayonetData> bayonetDataByDate = bayonetDataMapper.getBayonetDataByDate(dateTimeVo);
        return listBayonetToSankeyVo(bayonetDataByDate);
    }

    @Override
    public SankeyVo getBayonetByDate(DateTimeVo dateTimeVo, String name) {
        final List<BayonetData> bayonetDataByDateAndName = bayonetDataMapper.getBayonetDataByDateAndName(dateTimeVo, name);
        return listBayonetToSankeyVo(bayonetDataByDateAndName);
    }

    @Override
    public List<MainViewVo> getMainViewDataByDate(DateTimeVo dateTimeVo) {
        final List<MainViewVo> bayonetDataWithSumByDate = bayonetDataMapper.getBayonetDataWithSumByDate(dateTimeVo);
        return bayonetDataWithSumByDate;
    }

    private SankeyVo listBayonetToSankeyVo(List<BayonetData> bayonetDataByDate){
        Set<SankeyVo.Name> setNames = new HashSet<>();
        List<SankeyVo.Link> links = new ArrayList<>();
        bayonetDataByDate.forEach(bayonetData -> {
            final SankeyVo.Name nameBayonet = new SankeyVo.Name(bayonetData.getBayonetName());
            final SankeyVo.Name nameCarType = new SankeyVo.Name(bayonetData.getCarType());
            setNames.add(nameCarType);
            setNames.add(nameBayonet);
            final SankeyVo.Link link = new SankeyVo.Link(bayonetData.getBayonetName(), bayonetData.getCarType(), bayonetData.getCarNum());
            links.add(link);
        });
        return new SankeyVo(setNames, links);
    }
}
